<?php
/**
 * @file wapinfoaction.php
 * @author LinLin
 * @date 2016年11月15日 上午11:32:53
 * @brief 
 */
namespace haibao\cms\business\control\pc\action;
use haibao\cms\model\filter\Article as ArticleFilter;
use haibao\cms\model\filter\ArticleTag as ArticleTagFilter;
use haibao\cms\model\filter\imagecollective\ImageCollective as ImageCollFilter;
use haibao\cms\model\filter\imagecollective\ImageCollectiveTag as ImageCollTagFilter;
use haibao\frame\data\query\Condition;
use \haibao\cms\model\filter\WapInfo as WapInfoFilter;
use haibao\cms\model\filter\ImageLibrary as ImageLibraryFitler;
use haibao\cms\model\filter\imagecollective\ImageCollectivePro as ImageCollProFilter;
use haibao\cms\model\filter\ArticleProImage as ArticleProImageFilter;
use haibao\cms\model\filter\Video as VideoFilter;
use haibao\cms\model\data\VideoFile;

class WapInfoAction extends BaseAction{
    
    const QUESY_LIST_FOR_NEW = 1;
    
    const INFO_TYPE_ARTICLE = 1;//资讯类型 文章
    const INFO_TYPE_IMAGE = 2;//资讯类型 图集
    const INFO_TYPE_FEATURE = 3;//专题
    const INFO_TYPE_VIDEO = 4;//视频
    const INFO_TYPE_ARTICLE_PIC = 3;//资讯类型 文章读图模式
    
    const DATE_TYPE_WEEK = 1;// 1周
    const DATE_TYPE_MONTH = 2;// 1月
    const DATE_TYPE_HALFWEEK = 3;// 半月
    const DATE_TYPE_THREEDAY = 4;//3天之内
    const DATE_TYPE_TWO_DAYS_AGO = 5;//3天前
    const DATE_TYPE_LESSEQUAL_NOWTIME = 6;//小于等于当前时间
    
    const SORT_DESC = 'desc';
    const SORT_ASC = 'asc';
    
    public $wapInfoEntity;
    private static $_instance;
    
    private $pager;
    private $queryData;
    
    private $allArticleId = array();
    private $articleIdArr = array();
    private $collIdArr = array();
    private $videoIdArr = array();
    
    private $tagIdArr = array();
    private $collTagIdArr = array();
    
    private $articles = array();
    private $imageColls = array();
    
    private $articleProImages = array();
    private $collProImages = array();
    private $videoProImages = array();
    
    private $tagInfo = array();
    private $collTagInfo = array();
    
    private $articleTagArr = array();//文章关联标签数组
    private $collTagArr = array();//图集关联标签数组
    private $videoTagArr = array();//视频关联标签数组
    
    private $getArticleInTags = array();
    
    public static function initWapInfoAction(){
        if(!self::$_instance instanceof self){
            self::$_instance = new self;
            self::$_instance->queryData = new \haibao\cms\data\WapInfo();
        }
        self::$_instance->initVariable();
        return self::$_instance;
    }
    
    public function setWapInfoEnity($wapInfoEntity){
        $this->wapInfoEntity = $wapInfoEntity;
    }
    
    
    /**
     * WAP站获取资讯列表
     */
    public function getWapInfoList($channelType = null){
        
        $level = 4;
        if($channelType){
            $level = 3;
        }
        $wapInfoFilter = new WapInfoFilter();
        $wapInfoFilter->select(array(
            WapInfoFilter::CONDITION_FIELD_NAME_INFO_ID,
            WapInfoFilter::CONDITION_FIELD_NAME_TITLE,
            WapInfoFilter::CONDITION_FIELD_NAME_INFO_TYPE,
            WapInfoFilter::CONDITION_FIELD_NAME_TAG_ID,
            WapInfoFilter::CONDITION_FIELD_NAME_PUBLISH_ID,
            WapInfoFilter::CONDITION_FIELD_NAME_PUBLISH_TIME,
        ));
    
        $wapInfoFilter->where(WapInfoFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_GREATEREQUAL,$level);
    
        if($this->wapInfoEntity->getIsArticleTag()){
            if(is_array($this->wapInfoEntity->getTagIds())){
                $wapInfoFilter->where(WapInfoFilter::CONDITION_FIELD_NAME_TAG_ID,Condition::CONDITION_IN, $this->wapInfoEntity->getTagIds());
            }else{
                $wapInfoFilter->where(WapInfoFilter::CONDITION_FIELD_NAME_TAG_ID,Condition::CONDITION_EQUAL, (int)$this->wapInfoEntity->getTagIds());
            }
            //$wapInfoFilter->order(WapInfoFilter::CONDITION_FIELD_NAME_CHANNEL_TOP,false);
        }else{
    
            //$wapInfoFilter->order(WapInfoFilter::CONDITION_FIELD_NAME_INDEX_TOP,false);
        }
    
        $wapInfoFilter->where(WapInfoFilter::CONDITION_FIELD_NAME_PUBLISHED,Condition::CONDITION_EQUAL,1);
        $wapInfoFilter->where(WapInfoFilter::CONDITION_FIELD_NAME_PUBLISH_TIME,Condition::CONDITION_LESSEQUAL,date('Y-m-d H:i:s',time()));
        $wapInfoFilter->order(WapInfoFilter::CONDITION_FIELD_NAME_PUBLISH_TIME,false);
        $wapInfoFilter->limit(($this->wapInfoEntity->getCurrentPage()-1)*$this->wapInfoEntity->getLimit(),$this->wapInfoEntity->getLimit());
        $this->pager = $wapInfoFilter->getPager();
        $this->articles = $this->queryData->getAll($wapInfoFilter);

        $articleInfoArr = $imageCollInfoArr = array();
    
        if($this->articles){
            	
            foreach($this->articles as $info){
                if($info->InfoType == \haibao\cms\model\data\WapInfo::INFO_TYPE_ARTICLE){
                    array_push($this->articleIdArr,$info->InfoId);
                }elseif($info->InfoType == \haibao\cms\model\data\WapInfo::INFO_TYPE_IMAGE_COLLECTIVE){
                    array_push($this->collIdArr,$info->InfoId);
                }elseif($info->InfoType == \haibao\cms\model\data\WapInfo::INFO_TYPE_VIDEO){
                    array_push($this->videoIdArr,$info->InfoId);
                }
                array_push($this->allArticleId,$info->InfoId);
            }
            if($this->wapInfoEntity->getIsProImage()){
                	
                if($this->articleIdArr){
                    $this->articleProImages = $this->getArticleProImage($this->articleIdArr,$this->wapInfoEntity->getProImageLevel());
                    $this->articleTagArr = $this->getArticleTagByArticleId();
    
                    $articleFilter = new ArticleFilter();
                    $articleFilter->select(
                        array(
                            ArticleFilter::CONDITION_FIELD_NAME_ID,
                            ArticleFilter::CONDITION_FIELD_NAME_SHORTTITLE
                        )
                    );
                    $articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_IN,$this->articleIdArr);
                    $articleInfoArr  = $this->queryData->getAll($articleFilter);
                }
                if($this->collIdArr){
                    $this->collProImages = $this->getCollProImage($this->collIdArr, $this->wapInfoEntity->getProImageLevel());
                    $this->collTagArr = $this->getCollTagByCollId();
                    	
                    $imageCollFilter = new ImageCollFilter();
                    $imageCollFilter->select(
                        array(
                            ImageCollFilter::CONDITION_FIELD_NAME_ID,
                            ImageCollFilter::CONDITION_FIELD_NAME_SHORT_TITLE,
                        )
                    );
                    $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_IN,$this->collIdArr);
                    $imageCollInfoArr  = $this->queryData->getAll($imageCollFilter);
                }
                if($this->videoIdArr){
                    $this->videoProImages = $this->getVideoProImage($this->videoIdArr, $this->wapInfoEntity->getProImageLevel());
                    //$this->videoTagArr = $this->getCollTagByCollId();
                }
            }
            	
            if($channelType && $channelType == 'tv'){
                $this->getArticleInTags = \haibao\cms\business\ArticleTag::getTagsByArticleIds($this->allArticleId,false,array(11310,11311,11312));
            }
        }
        return array($this->articles,$articleInfoArr,$this->articleProImages,$imageCollInfoArr,$this->collProImages,$this->articleTagArr,$this->collTagArr,$this->getArticleInTags,$this->videoProImages);
    }

    /**
     * @param Array $collId 外部调用传递的文章ID
     * @param Boolure $isReturnPrimaryTag
     */
    public function getCollTagByCollId($collId = '',$isReturnPrimaryTag = true){
        $collTagArr = array();
    
        $imageCollTagFilter = new ImageCollTagFilter();
        $imageCollTagFilter->select(
            array(
                ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,
                ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ID,
                ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_PARENTID,
                ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ISPRIMAIY,
            )
        );
        if($collId){
            $collId = (array)$collId;
            $imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,Condition::CONDITION_IN,$collId);
        }else{
    
            if(!$this->collIdArr){
                return $collTagArr;
            }
            $imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,Condition::CONDITION_IN,$this->collIdArr);
        }
    
        $collTags = $this->queryData->getAll($imageCollTagFilter);
        $collTagArr = array();
        if($collTags){
            foreach($collTags as $collTag){
    
                if($isReturnPrimaryTag){
                    if($collTag->IsPrimaiy == \haibao\cms\model\data\imagecollective\ImageCollectiveTag::IS_PRIMAIY){
                        $collTagArr[$collTag->ImageCollectiveId]['TagId'] = $collTag->TagId;
                        $collTagArr[$collTag->ImageCollectiveId]['ParentId'] = $collTag->ParentId;
                    }
                }else{
    
                    if(!isset($collTagArr[$collTag->ImageCollectiveId])){
                        $collTagArr[$collTag->ImageCollectiveId] = array();
                    }
    
                    $temp = array();
                    $temp['TagId'] = $collTag->TagId;
                    $temp['ParentId'] = $collTag->ParentId;
                    $temp['IsPrimaiy'] = $collTag->IsPrimaiy;
                    $collTagArr[$collTag->ImageCollectiveId][] = $temp;
                    	
                }
            }
        }
        foreach($collTagArr as $key=>$val){
            if(empty($val)) $collTagArr[$key] = self::HAI_BAO_WANG_TAG;
        }
        return $collTagArr;
    }
    
    /**
     * @param Array $articleId 外部调用传递的文章ID
     * @param Boolure $isReturnPrimaryTag
     */
    public function getArticleTagByArticleId($articleId = '',$isReturnPrimaryTag = true){
    
       $articleTagFilter = new ArticleTagFilter();
		$articleTagFilter->select(
				array(
						ArticleTagFilter::CONDITION_FIELD_NAME_ARTICLE_ID,
						ArticleTagFilter::CONDITION_FIELD_NAME_TAG_ID,
						ArticleTagFilter::CONDITION_FIELD_NAME_PARENT_ID,
						ArticleTagFilter::CONDITION_FIELD_NAME_IS_PRIMAIY,
				)
		);
		if($articleId){
			$articleId = (array)$articleId;
			$articleTagFilter->where(ArticleTagFilter::CONDITION_FIELD_NAME_ARTICLE_ID,Condition::CONDITION_IN,$articleId);
		}else{
			$articleTagFilter->where(ArticleTagFilter::CONDITION_FIELD_NAME_ARTICLE_ID,Condition::CONDITION_IN,$this->articleIdArr);
		}
	
		$articleTags = $this->queryData->getAll($articleTagFilter);
		$articleTagArr = array();
		if($articleTags){
			foreach($articleTags as $articleTag){
	
				if(!isset($articleTagArr[$articleTag->ArticleId])){
					$articleTagArr[$articleTag->ArticleId] = array();
				}
				if($isReturnPrimaryTag){
					if($articleTag->IsPrimaiy == \haibao\cms\model\data\ArticleTag::PRIMAIY_IS){
						$articleTagArr[$articleTag->ArticleId]['TagId'] = $articleTag->TagId;
						$articleTagArr[$articleTag->ArticleId]['ParentId'] = $articleTag->ParentId;
					}
					
				}else{
	
					$temp = array();
					$temp['TagId'] = $articleTag->TagId;
					$temp['ParentId'] = $articleTag->ParentId;
					$temp['IsPrimaiy'] = $articleTag->IsPrimaiy;
					if(!isset($articleTagArr[$articleTag->ArticleId])) $articleTagArr[$articleTag->ArticleId] = array();
					$articleTagArr[$articleTag->ArticleId][] = $temp;
				}
			}
		}
		
		foreach($articleTagArr as $key=>$val){
			if(empty($val)) {
				$articleTagArr[$key]['TagId'] = self::HAI_BAO_WANG_TAG;
				$articleTagArr[$key]['ParentId'] = self::HAI_BAO_WANG_TAG;
			}

		}
		return $articleTagArr;
    }
    /**
     * 获取视频导读图
     */
    public function getVideoProImage($videoIdArr,$proImageLevel){
        if(!is_array($videoIdArr)) $videoIdArr = (array)$videoIdArr;
        $videoFilter = new VideoFilter();
        $videoFilter->select(array(
            VideoFilter::CONDITION_FIELD_NAME_ID,
            VideoFilter::CONDITION_FIELD_NAME_SQUAREPICURL,
            VideoFilter::CONDITION_FIELD_NAME_PICID
        ));
        $imageLibraryFilter = new ImageLibraryFitler();
        $imageLibraryFilter->select(array(
            ImageLibraryFitler::CONDITION_FIELD_NAME_FILENAME,
        ));
        $videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_IN,$videoIdArr);
        $videoFilter->leftJoin($imageLibraryFilter,null,$videoFilter::CONDITION_FIELD_NAME_PICID,ImageLibraryFitler::CONDITION_FIELD_NAME_ID,$videoFilter::CONDITION_FIELD_NAME_IMAGE,false);
        $videoProImages = $this->queryData->getAll($videoFilter);
        
        $proImageArr = array();
        foreach($videoProImages as $proImage){
            if(isset($proImage->SquarePicUrl)){
                $proImageArr[$proImage->Id] = $proImage->SquarePicUrl;
            }else{
                $proImageArr[$proImage->Id] = $proImage->Image->Filename;
            }
        }
        return $proImageArr;
    }
    /**
     * 获取图集导读图
     * @param : Array $articleIdArr 文章ID数组
     * @param : Array $level 导读图等级
     *
     */
    public function getCollProImage($collIdArr,$proImageLevel){
        if(!is_array($proImageLevel)) $proImageLevel = (array)$proImageLevel;
        if(!is_array($collIdArr)) $collIdArr = (array)$collIdArr;
        $imageCollProFilter = new ImageCollProFilter();
        $imageCollProFilter->select(
            array(
                ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,
                ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_IMAGE_TYPE,
                ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_ID,
            )
        );
        $imageLibraryFilter = new ImageLibraryFitler();
        $imageLibraryFilter->select(array(
            ImageLibraryFitler::CONDITION_FIELD_NAME_FILENAME,
        ));
        array_push($proImageLevel,\haibao\cms\model\data\imagecollective\ImageCollectivePro::IMAGE_TYPE_ZERO);
    
        $imageCollProFilter->where(ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_IMAGE_TYPE,Condition::CONDITION_IN,$proImageLevel);
        $imageCollProFilter->where(ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,Condition::CONDITION_IN,$collIdArr);
        $imageCollProFilter->leftJoin($imageLibraryFilter,null,ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_ID,ImageLibraryFitler::CONDITION_FIELD_NAME_ID,ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_FILENAME,false);
    
        $collProImages = $this->queryData->getAll($imageCollProFilter);
    
        $proImageArr = array();
        foreach($collProImages as $proImage){
            if(isset($proImageArr[$proImage->ImageCollectiveId])){
                $proImageArr[$proImage->ImageCollectiveId][$proImage->ImageType] = $proImage->FileName->Filename;
            }else{
                $proImageArr[$proImage->ImageCollectiveId] = array();
                $proImageArr[$proImage->ImageCollectiveId][$proImage->ImageType] = $proImage->FileName->Filename;
            }
        }
        return $proImageArr;
    }
    
    /**
     * 获取文章导读图
     * @param : Array $articleIdArr 文章ID数组
     * @param : Array $level 导读图等级
     */
    public function getArticleProImage($articleIdArr,$proImageLevel = ''){
    
        if(!is_array($proImageLevel)) $proImageLevel = (array)$proImageLevel;
        array_push($proImageLevel,\haibao\cms\model\data\ArticleProImage::Level_ZERO);
        $articleProImageFilter = new ArticleProImageFilter();
        $articleProImageFilter->select(
            array(
                ArticleProImageFilter::CONDITION_FIELD_NAME_ARTICLE_ID,
                ArticleProImageFilter::CONDITION_FIELD_NAME_LEVEL,
                ArticleProImageFilter::CONDITION_FIELD_NAME_PIC_URL,
            )
        );
        $articleProImageFilter->where(ArticleProImageFilter::CONDITION_FIELD_NAME_PIC_TYPE,Condition::CONDITION_EQUAL,\haibao\cms\model\data\ArticleProImage::PIC_TYPE_INTRO);
    
        $articleProImageFilter->where(ArticleProImageFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_IN,$proImageLevel);
        $articleProImageFilter->where(ArticleProImageFilter::CONDITION_FIELD_NAME_ARTICLE_ID,Condition::CONDITION_IN,$articleIdArr);
    
        $articleProImages = \haibao\cms\business\Article::getArticleList($articleProImageFilter);
        $proImageArr = array();
        $articleIds = array();
        foreach($articleProImages as $proImage){
            if(isset($proImageArr[$proImage->ArticleId])){
                $proImageArr[$proImage->ArticleId][$proImage->Level] = $proImage->PicUrl;
            }else{
                $proImageArr[$proImage->ArticleId] = array();
                $proImageArr[$proImage->ArticleId][$proImage->Level] = $proImage->PicUrl;
            }
            $articleIds[] = $proImage->ArticleId;
        }
    
        $articleDiffIds = array_diff($this->articleIdArr, $articleIds);
        if($articleDiffIds){
            foreach ($articleDiffIds as $articleId){
                $proImageArr[$articleId] = array();
                $proImageArr[$articleId][\haibao\cms\model\data\ArticleProImage::Level_ZERO] = \haibao\cms\business\control\PublicController::getArticleFirstImage($articleId);
            }
        }
        return $proImageArr;
    }
    
    
    
    private function packageReturnData(){
    
        $article = array(
            'list'=>$this->articles,
            'proImage'=>$this->articleProImages,
            'tag'=>$this->tagInfo,
            'articleTag'=>$this->articleTagArr
    
        );
        $imageColl = array(
            'list'=>$this->imageColls,
            'proImage'=>$this->collProImages,
            'tag'=>$this->collTagInfo,
            'collTag'=>$this->collTagArr
        );
        return array($article,$imageColl);
    }
    /**
     * 检验实体
     */
    public function checkEntity(){
        if($this->wapInfoEntity instanceof WapInfoAction){
            return true;
        }
        return false;
    }
    
    private function initVariable(){
        $this->articleIdArr = array();
        $this->collIdArr = array();
    
        $this->tagIdArr = array();
        $this->collTagIdArr = array();
    
        $this->articles = array();
        $this->imageColls = array();
    
        $this->articleProImages = array();
        $this->collProImages = array();
    
        $this->tagInfo = array();
        $this->collTagInfo = array();
    
        $this->articleTagArr = array();//文章关联标签数组
        $this->collTagArr = array();//图集关联标签数组
    }
}